<?php

$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
// 当WebSocket客户端与服务器建立连接并完成握手后会回调此函数
$server->on('open', function (Swoole\WebSocket\Server $server, Swoole\Http\Request $request) {
    $server->push($request->fd,'欢迎进入我们的聊天室');
});



// 当服务器收到来自客户端的数据帧时会回调此函数  此回调方法不能缺少
$server->on('message', function (Swoole\WebSocket\Server $server, swoole_websocket_frame $frame) {
    $data = $frame->data;
    $ret['data'] = $data;
    foreach ($server->connections as $client){
        if ($frame->fd == $client ){
            $ret['style'] = 'bubble me';
        }else{
            $ret['style'] = 'bubble you';
        }
        @$server->push($client,json_encode($ret,256));
    }
});
// 客户端关闭连接时触发此回调函数
$server->on('close', function ($ser, $fd) {

});
// 启动服务
$server->start();
